﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 门诊管理系统
{
    public partial class patient : Form
    {

     


        public patient()
        {
            InitializeComponent();

        }


        private void btn_show_Click(object sender, EventArgs e)
        {

            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                 ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_patient;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;


            DataTable patientTable = new DataTable();

            sqlDataAdapter.Fill(patientTable);

            sqlConnection.Close();

            this.dgv_patient.Columns.Clear();
            this.dgv_patient.DataSource = patientTable;
            this.dgv_patient.Columns["IDcard"].HeaderText = "就诊卡号";
            this.dgv_patient.Columns["name"].HeaderText = "姓名";
            this.dgv_patient.Columns["sex"].HeaderText = "性别";
            this.dgv_patient.Columns["birthdate"].HeaderText = "出生日期";
            this.dgv_patient.Columns["ID"].HeaderText = "身份证";
            this.dgv_patient.Columns["phone"].HeaderText = "联系电话";
            this.dgv_patient.Columns["casehistory"].HeaderText = "病例";
            this.dgv_patient.Columns["Photo"].Visible = false;
            this.dgv_patient.Columns[this.dgv_patient.Columns.Count - 2].AutoSizeMode =
               DataGridViewAutoSizeColumnMode.Fill;

        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                 ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            SqlCommand insertCommand = new SqlCommand();


            insertCommand.Connection = sqlConnection;
            insertCommand.CommandText =
                "INSERT tb_patient"
                + "(IDcard,name,sex,birthdate,ID,phone,casehistory)"
                + " VALUES(@IDcard,@name,@sex,@birthdate,@ID,@phone,@casehistory);";
            insertCommand.Parameters.Add("@IDcard", SqlDbType.VarChar, 50, "IDcard");
            insertCommand.Parameters.Add("@name", SqlDbType.VarChar, 10, "name");
            insertCommand.Parameters.Add("@sex", SqlDbType.VarChar, 0, "sex");
            insertCommand.Parameters.Add("@birthdate", SqlDbType.VarChar, 0, "birthdate");
            insertCommand.Parameters.Add("@ID", SqlDbType.VarChar, 50, "ID");
            insertCommand.Parameters.Add("@phone", SqlDbType.VarChar, 20, "phone");
            insertCommand.Parameters.Add("@casehistory", SqlDbType.VarChar, 20, "casehistory");

            SqlCommand updateCommand = new SqlCommand();
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =
                "UPDATE tb_patient"
                + " SET IDcard=@NewIDcard,name=@name,sex=@sex,birthdate=@birthdate,ID=@ID,phone=@phone"
                + " WHERE IDcard=@OldIDcard;";
            updateCommand.Parameters.Add("@IDcard", SqlDbType.VarChar, 50, "IDcard");
            updateCommand.Parameters.Add("@name", SqlDbType.VarChar, 10, "name");
            updateCommand.Parameters.Add("@sex", SqlDbType.VarChar, 0, "sex");
            updateCommand.Parameters.Add("@birthdate", SqlDbType.VarChar, 0, "birthdate");
            updateCommand.Parameters.Add("@ID", SqlDbType.VarChar, 50, "ID");
            updateCommand.Parameters.Add("@phone", SqlDbType.VarChar, 20, "phone");
            updateCommand.Parameters.Add("@OldIDcard", SqlDbType.Char, 50, "IDcard");
            updateCommand.Parameters["@OldIDcard"].SourceVersion = DataRowVersion.Original;

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.InsertCommand = insertCommand;
            sqlDataAdapter.UpdateCommand = updateCommand;
            DataTable patientTable1 = (DataTable)this.dgv_patient.DataSource;
            sqlConnection.Open();
            int rowAffected = sqlDataAdapter.Update(patientTable1);
            sqlConnection.Close();


            MessageBox.Show("更新" + rowAffected.ToString() + "行。");
            casehistory c = new casehistory();
            c.Show();
        }

    }
}